package demo;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Author: WWQ
 * @Date: 2019/4/8 11:35
 * @Version 1.0
 */
public class StreamAPITest4 {
    List<Employee> emps = Arrays.asList(
            new Employee("李四", 59, 6666.66),
            new Employee("张三", 18, 9999.99),
            new Employee("王五", 28, 3333.33),
            new Employee("赵六", 8, 7777.77),
            new Employee("赵六", 8, 7777.77),
            new Employee("赵六", 8, 7777.77),
            new Employee("田七", 38, 5555.55)
    );

    //2. 中间操作
	/*
	    排序
		sorted（）自然排序
        sorted（Comparator com）定制排序
	 */


    /**
     * 自然排序
     */
    @Test
    public void test1(){
        Stream<Integer> stream=emps.stream().map((e)->e.getAge()).sorted();
        stream.forEach(System.out::println);
    }

    /**
     * 定制排序
     */
    @Test
    public void test2(){
        Stream<Employee> stream=emps.stream().sorted((x,y)->{
           if(x.getAge()==y.getAge()){
               return x.getName().compareTo(y.getName());
           }else{
               return Integer.compare(x.getAge(),y.getAge());
           }
        });
        stream.forEach(System.out::println);
    }
}
